package com.lcm.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

     private static final int ADD_STUDENT = 1;
    private static final int DELETE_STUDENT = 2;
    private static final int UPDATE_STUDENT = 3;
    private static final int QUERY_STUDENT = 4;
    private static final int EXIT = 5;
    public static void startStudentSystem( ) {
        ArrayList<Student> list = new ArrayList<>();
       loop: while (true){
            System.out.println("--------------欢迎来到梦梦学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入您的选择");
            int choose = sc.nextInt();
            switch (choose){
                case ADD_STUDENT:
                    addStudent(list);
                      break ;
                case DELETE_STUDENT:
                    deleteStudent(list);
                    break ;
                case UPDATE_STUDENT:
                    updateStudent(list);
                    break ;
                case QUERY_STUDENT:
                    queryStudent(list);
                    break ;
                case EXIT:{
                    System.out.println("5：退出");
                    break loop;
//                    System.exit(0);//停止虚拟机运行
                }
                default:
                    System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent( ArrayList<Student> list){
        Scanner scanner = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学生的id：");
            sid = scanner.next();
            boolean flag = contains(list,sid);
            if (flag){
                System.out.println("id已存在，请重新录入：");
            }else {
                break;
            }
        }
        System.out.println("请输入学生的名字：");
        String name = scanner.next();
        System.out.println("请输入学生的年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入学生的家庭住址：");
        String address = scanner.next();
        Student student = new Student(sid,name,age,address);
        list.add(student);
        System.out.println("添加成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请你输入你要删除的学生id：");
        String id = sc.next();
       boolean flag =  contains(list,id);
       if (flag){
          int index = getIndex(list,id);
          list.remove(index);
           System.out.println("删除成功");
       }else {
           System.out.println("不存在");
       }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生id：");
        String iid = sc.next();
        int index = getIndex(list,iid);
        if (index==-1){
            System.out.println("不存在");
            return;
        }
        Student student = list.get(index);
        System.out.println("请输入你要修改的学生姓名：");
        String newName = sc.next();
        student.setName(newName);
        System.out.println("请输入你要修改的学生年龄：");
        int newAge = sc.nextInt();
        student.setAge(newAge);
        System.out.println("请输入你要修改的学生家庭住址：");
        String newAddress = sc.next();
        student.setAddress(newAddress);
        System.out.println("修改成功");
    }

    //查询学生
    public static void queryStudent( ArrayList<Student> list){
        System.out.println("id" +"\t"+"姓名"+"\t"+"年龄"+"\t"+"家庭住址");
        if (list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }

    //判断集合中是否存在此id
    public static boolean contains(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String idd = stu.getId();
            if (idd.equals(id)){
                return true;
            }
        }
        return false;
    }

    //定义一个方法通过id获取索引
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}